app.controller('categoryManagerController', ['$scope', 'ajax','uploadService','$state', function ($scope, ajax,uploadService,$state) {

    $scope.dataSource = [{}, {}];

    $scope.data = {
        isShowAddView: false,
        isShowUpdateView: false
    };

    //新增数据绑定
    $scope.newItem = {
        level: "",
        name: "",
        parentId: ""
    };

    //修改数据绑定
    $scope.updateItem = {
        categoryId: "",
        level: "",
        name: "",
        parentId: ""
    };

    function didSelectItem(lvl, index) {

        if (lvl == 1) return;

        var data = $scope.dataSource[lvl];

        var cat = data[index];
        for (var i = 0; i < data.length; i++) {

            if (i == index) {

                if (!data[i].active) {
                    data[i].active = true;
                }

                //重新加载服务端的数据
                getItemList(data[i], function (retData) {

                });

            } else {
                if (data[i].active) {
                    data[i].active = false;
                }
            }

        }

    };

    $scope.clickCat = function (lvl, index) {
        didSelectItem(lvl - 1, index);
    };

    getItemList(null, function (retData) {

    });

    //获取指定categoryId的子列表,查询cat.level级类目
    function getItemList(cat, completeCallback) {
        if (cat == null) {

            cat = {
                level: 0,
                categoryId: ""
            }

        }

        ajax.get({
            url: "back/commodityCategory/list",
            data: {
                categoryId: "",
                level: parseInt(cat.level) + 1,
                name: "",
                parentId: cat.categoryId
            }
        }).then(function (data) {

            var arr = new Array();

            for (var i = 0; i < data.length; i++) {

                var newCat = {
                    categoryId: data[i].categoryId,
                    level: data[i].level,
                    name: data[i].name,
                    parentId: data[i].parentId,
                    imageUrl:data[i].imageUrl,
                    imageId:data[i].imageId,
                    webShow:data[i].webShow,
                    active: false
                };


                arr.push(newCat);
            }

            $scope.dataSource[parseInt(cat.level)] = arr;

            if (cat.level == 0) {
                $scope.dataSource[1] = null;
            }

            completeCallback(arr);


        });

    }


    //新增
    $scope.showAddView = function (level) {

        $scope.data.isShowAddView = false;
        $scope.data.isShowUpdateView = false;
        $scope.pic = '';

        //初始化数据
        if (level == 0) {

                $scope.newItem = {
                    level: "1",
                    name: "",
                    parentId: "0"
                };

            $scope.data.isShowAddView = true;

        } else {

            //判断当前level1是否有active
            var arr = $scope.dataSource[0];
            for (var i = 0; i < arr.length; i++) {
                if (arr[i].active) {
                    $scope.newItem = {
                        level: "2",
                        name: "",
                        parentId: arr[i].categoryId
                    };
                    $scope.data.isShowAddView = true;
                    return;
                }
            }

            alert("请选择一级类目");

        }

    }

    $scope.showUpdateView = function (level, index) {
        $scope.data.isShowAddView = false;
        $scope.data.isShowUpdateView = true;
        $scope.pic = '';

        var item = $scope.dataSource[level][index];
        $scope.updateItem = {
            categoryId: item.categoryId,
            level: item.level,
            name: item.name,
            parentId: item.parentId
        };
    };

    //提交新增
    $scope.doAddItem = function () {
        $scope.newItem.imageUrl = $scope.imageUrl;
        $scope.newItem.imageId = $scope.imageId;
        console.log(11111,$scope.newItem);
        ajax.post({
            url: "back/commodityCategory/add",
            data: $scope.newItem
        }).then(function () {
            $scope.data.isShowAddView = false;
            $scope.data.isShowUpdateView = false;

            if ($scope.newItem.level == 1) {
                getItemList(null, function (retData) {

                });
            } else {
                //获取当前选中的item
                var arr = $scope.dataSource[0];

                for (var i = 0; i < arr.length; i++) {

                    if (arr[i].active) {

                        getItemList(arr[i], function (retData) {


                        });

                    }

                }

            }


        });

    };

    //提交更新
    $scope.doUpdateItem = function () {
        $scope.updateItem.imageUrl = $scope.imageUrl;
        $scope.updateItem.imageId = $scope.imageId;
        ajax.post({
            url: "back/commodityCategory/update",
            data: $scope.updateItem
        }).then(function (result) {
            $scope.data.isShowAddView = false;
            $scope.data.isShowUpdateView = false;

            if ($scope.updateItem.level == 1) {
                getItemList(null, function (retData) {
                });
            } else {
                //获取当前选中的item
                var arr = $scope.dataSource[0];
                for (var i = 0; i < arr.length; i++) {
                    if (arr[i].active) {
                        getItemList(arr[i], function (retData) {
                        });

                    }

                }
            }

        });

    };

    //删除item
    $scope.doDeleteItem = function (level, index) {
        $scope.data.isShowAddView = false;
        $scope.data.isShowUpdateView = false;

        var item = $scope.dataSource[level][index];
        ajax.post({
            url: "back/commodityCategory/delete",
            data: {
                categoryIds: item.categoryId
            }
        }).then(function (result) {

            if (item.level == 1) {
                getItemList(null, function (retData) {

                });
            } else {
                //获取当前选中的item
                var arr = $scope.dataSource[0];

                for (var i = 0; i < arr.length; i++) {

                    if (arr[i].active) {

                        getItemList(arr[i], function (retData) {


                        });

                    }

                }
            }

        });

    };
    //是否显示
    $scope.doShowItem = function (categoryId,webShow){
        ajax.post({
            url: "back/commodityCategory/updateWebShow",
            data: {
                categoryId: categoryId,
                webShow:webShow
        }
        }).then(function () {

            didSelectItem(0, 0);
            $state.go('admin.categoryList',null,{
                reload:true
            });
        });
    }

    //上传图片
    $scope.uploadPic = function (data) {
        uploadService.upload(data).then(function (result) {
            alert('上传成功');
            $scope.imageUrl = result.url;
            $scope.imageId = result.attachmentId;
        });
    }


}]);